package com.dj.mingkong.admin.biz.service.music.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.music.vo.request.BoxQueryVo;
import com.dj.mingkong.admin.biz.controller.music.vo.request.BoxUpdateVo;
import com.dj.mingkong.admin.biz.controller.music.vo.response.BoxVo;
import com.dj.mingkong.admin.biz.repository.database.music.BoxRepository;
import com.dj.mingkong.admin.biz.service.music.BoxService;
import com.dj.mingkong.common.infrastructure.base.dto.delete.DeleteVo;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.eo.database.music.BoxEo;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.common.util.SmartBeanCopy;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 音乐盒管理服务实现
 *
 * @author zenglonghui
 * @date: 2022/12/13 16:58
 */
@Slf4j
@Service("boxService")
public class BoxServiceImpl implements BoxService {
    @Resource
    private BoxRepository boxRepository;

    @Override
    public PageVo<BoxVo> pageByVo(BoxQueryVo queryVo) {
        final Page<BoxVo> boxVoPage = boxRepository.getMapper()
                .listPage(Page.of(queryVo.getPage(), queryVo.getPageSize()), queryVo);
        return SmartBeanCopy.copyPageVo(boxVoPage, BoxVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(BoxUpdateVo updateVo) {
        final BoxEo boxEo = boxRepository.getById(updateVo.getId());
        AssertUtils.isNotNull(boxEo, "id invalid!");
        boxEo.setStatus(updateVo.getStatus()).setGenStatus(updateVo.getGenStatus());
        boxRepository.updateById(boxEo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(DeleteVo deleteVo) {
        boxRepository.deleteById(deleteVo.getId());
    }

}
